;layui.define(function (e) {
    layui.use(['index', 'jquery', 'http', 'common', 'table', 'form', 'layer'], function () {

        var $ = layui.jquery
            , http = layui.http
            , table = layui.table
            , form = layui.form
            , common = layui.common
            , layer = parent.layer === undefined ? layui.layer : top.layer;

        var urls = {
            queryPageData: "/core/schedule/queryPageData",
            addData: "/core/schedule/addData",
            modifyData: "/core/schedule/modifyData",
            pause: "/core/schedule/pause",
            resume: "/core/schedule/resume",
            remove: "/core/schedule/remove",
            runNow: "/core/schedule/runNow",
        };
        var url;
        var tableIns;
        var index;
        queryPageData();

        function queryPageData () {
            //第一个实例
            tableIns = http.table({
                elem: '#dataList'
                , height: 'full-100'
                , skin: 'rows'
                , page: {curr: 1}//重新从第 1 页开始
                , url: urls.queryPageData //数据接口
                , cols: [[ //表头
                    {field: 'jobName', title: '任务名称', minWidth: 120}
                    , {field: 'jobGroup', title: '任务分组', minWidth: 140}
                    , {field: 'jobDescription', title: '任务描述', minWidth: 120}
                    , {
                        field: 'jobStatus', title: '任务状态', minWidth: 120, templet: function (d) {
                            var s = '';
                            if (d.jobStatus == 'PAUSED') {
                                s = '<span style="color:#FF5722">暂停</span>';
                            } else {
                                s = '<span style="color:#1E9FFF">运行中</span>';
                            }
                            return s;
                        }
                    }
                    , {field: 'cronExpression', title: '任务表达式', minWidth: 120}
                    , {field: 'createTime', title: '创建时间', minWidth: 120}
                    , {title: '操作', fixed: 'right', align: "center", minWidth: 100, templet: '#opTpl'}
                ]]
            });
        }

        form.on("submit(modifyData)", function (data) {
            http.post({
                loading: true,
                loadingText: '数据提交中,请稍后...',
                url: url,
                data: data.field,
                success: function (res) {
                    fn.refresh();
                    common.showSuccessTips('权限保存成功!')
                }
            });
            return false;
        });

        table.on('tool(dataList)', function (obj) {
            var layEvent = obj.event,
                data = obj.data;
            if (layEvent == 'del') {
                layer.confirm('删除该任务将会删除任务相关的数据,确定删除吗？', {icon: 3, title: '提示信息'}, function () {
                    http.post({
                        loading: true,
                        loadingText: '数据提交中,请稍候...',
                        url: urls.deleteData + "/" + data.id,
                        data: {},
                        success: function () {
                            common.showSuccessTips('任务删除成功', function () {
                                fn.refresh();
                            });
                        }
                    });
                });
            } else if (layEvent == 'edit') {
                url = urls.modifyData;
                openEditDialog('修改任务', data);
            } else if (layEvent == 'start') {
                layer.confirm('你确定要启动该定时任务吗？', {icon: 3, title: '提示信息'}, function () {
                    http.post({
                        loading: true,
                        loadingText: '任务启动中,请稍候...',
                        url: urls.resume,
                        data: data,
                        success: function () {
                            common.showSuccessTips('任务启动成功', function () {
                                fn.refresh();
                            });
                        }
                    });
                });
            } else if (layEvent == 'stop') {
                layer.confirm('你确定要停止该定时任务吗？', {icon: 3, title: '提示信息'}, function () {
                    http.post({
                        loading: true,
                        loadingText: '任务停止中,请稍候...',
                        url: urls.pause,
                        data: data,
                        success: function () {
                            common.showSuccessTips('任务停止成功', function () {
                                fn.refresh();
                            });
                        }
                    });
                });
            } else if (layEvent == 'runNow') {
                layer.confirm('你确定要立即运行一次该定时任务吗？', {icon: 3, title: '提示信息'}, function () {
                    http.post({
                        loading: true,
                        loadingText: '任务立即运行中,请稍候...',
                        url: urls.runNow,
                        data: data,
                        success: function () {
                            common.showSuccessTips('任务立即运行成功', function () {
                                fn.refresh();
                            });
                        }
                    });
                });
            }
        });

        table.on('sort(dataList)', function (obj) { //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
            tableIns.reload();
        });

        fn.refresh = function () {
            if (index)
                layui.layer.close(index);
            tableIns.reload();
        };

        $("#btnQuery").on("click", function () {
            fn.refresh();
        });

        $("#btnAddPage").click(function () {
            url = urls.addData;
            openEditDialog('添加任务', {});
        });

        var openEditDialog = function (title, data) {
            index = layui.layer.open({
                type: 1,
                title: title,
                shade: false,
                maxmin: true,
                area: ['550px', '450px'],
                content: $('#editTpl').html(),
                btn: ['保存', '关闭'],
                btnAlign: 'c',
                yes: function (index, layero) {
                    layero.find("#scheduleForm").find("#btnAdd").click();
                },
                btn2: function (index) {
                    layui.layer.close(index);
                },
                success: function (layero, index) {
                    form.val("scheduleForm", data);
                }
            });
        };
    });

    e("core/schedule", {})
});